<template>
  <div class="app-container">
    <div class="top">
      <div class="operate">
        <el-button
          v-hasPermi="['indexMgmt:configuration:config']"
          type="primary"
          size="small"
          :disabled="single != null"
          @click="handleUpdate()"
          >配置</el-button
        >
      </div>
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item>
          <organizationCascader
            :clearData="{
              factory: true,
              workshop: true,
              line: true,
            }"
            :arrData="queryParams"
            :isAllList="true"
            @change="queryParamsChange"
          ></organizationCascader>
        </el-form-item>
        <el-form-item prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入关键字"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="Table"
      :data="list"
      tooltip-effect="dark"
      style="width: 100%"
      height="calc(100% - 44px - 50px)"
      v-loading="loading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="指标编码">
        <template slot-scope="{ row }">
          <el-button type="text" @click="handleShowDetail(row)">{{
            row.indexConfigCode
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="指标名称"
        prop="indexConfigName"
      ></el-table-column>
      <el-table-column label="所属元指标">
        <template slot-scope="{ row }">
          {{ row.indexMetaDto ? row.indexMetaDto.indexName : "" }}
        </template>
      </el-table-column>
      <el-table-column label="时间维度">
        <template slot-scope="{ row }">
          {{
            row.indexMetaDto
              ? timeDimensionDictMap[row.indexMetaDto.timeDimension]
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="组织维度">
        <template slot-scope="{ row }">
          {{
            row.indexMetaDto
              ? bizOrgDimensionDictMap[row.indexMetaDto.bizOrgDimension]
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="所属工厂" prop="factoryDeptName">
      </el-table-column>
      <el-table-column label="所属车间" prop="workshopName"> </el-table-column>
      <el-table-column label="所属产线" prop="lineName"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="{ row }">
          <span :class="'status-' + row.disableFlag">{{
            row.disableFlag == "Y" ? "禁用" : "启用"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right">
        <template slot-scope="{ row }">
          <el-button
            v-hasPermi="['indexMgmt:configuration:edit']"
            type="text"
            @click.stop="handleUpdate(row)"
            >编辑</el-button
          >
          <el-button
            v-if="row.disableFlag == 'N'"
            v-hasPermi="['indexMgmt:configuration:stop']"
            type="text"
            @click.stop="handleDisabled(row)"
            >禁用</el-button
          >
          <el-button
            v-if="row.disableFlag == 'Y'"
            v-hasPermi="['indexMgmt:configuration:start']"
            type="text"
            @click.stop="handleDisabled(row)"
            >启用</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="pager.total"
      :page.sync="pager.pageNum"
      :limit.sync="pager.pageSize"
      @pagination="getList"
    />

    <DetailDialog
      :visible.sync="detailDialog.visible"
      :row="detailDialog.row"
      :disabled="detailDialog.disabled"
      @submit-success="getList"
    ></DetailDialog>
  </div>
</template>
<script>
import organizationCascader from "@/views/components/organizationCascader";
import {
  getIndexConfigList,
  disabledIndexConfig,
} from "@/api/indexMgmt/configuration";
import DetailDialog from "./detailDialog.vue";
export default {
  name: "IndexConfiguration",
  components: { organizationCascader, DetailDialog },
  dicts: ["indexBizOrgDimension", "indexTimeDimension"],
  data() {
    return {
      queryParams: {
        factoryDeptCode: null,
        workshopId: null,
        workshopCode: null,
        lineId: null,
        searchValue: null,
      },
      list: [],
      single: null,
      loading: false,
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      detailDialog: {
        visible: false,
        row: null,
        disabled: false,
      },
    };
  },
  computed: {
    bizOrgDimensionDictMap() {
      return this.dictToMap(this.dict.indexBizOrgDimension);
    },
    timeDimensionDictMap() {
      return this.dictToMap(this.dict.indexTimeDimension);
    },
  },
  created() {
    // 默认获取第一个工厂-车间-产线
    const results = this.getOwnFirstLine();
    this.queryParams.factoryDeptCode = results[0]?.factoryDeptCode || "";
    this.queryParams.workshopId = results[1]?.workshopId || "";
    this.queryParams.workshopCode = results[1]?.workshopCode || "";
    this.queryParams.lineId = results[2]?.lineId || "";

    this.getList();
  },
  methods: {
    handleSelectionChange(selection) {
      if (selection.length > 1) {
        this.single = false;
      } else if (selection.length == 1) {
        this.single = true;
      } else {
        this.single = null;
      }
    },
    getList() {
      this.loading = true;
      const { pageNum, pageSize } = this.pager;
      const { factoryDeptCode, workshopId, lineId, searchValue } =
        this.queryParams;
      const param = {
        query: {
          factoryDeptCode,
          workshopId,
          lineId,
          searchValue,
        },
        pageNum,
        pageSize,
      };
      getIndexConfigList(param)
        .then((res) => {
          this.list = res?.rows || {};
          this.pager.total = res?.total || 0;
          this.loading = false;
        })
        .catch(() => {
          this.list = [];
          this.pager.total = 0;
          this.loading = false;
        });
    },
    queryParamsChange({ factoryDeptCode, workshopId, workshopCode, lineId }) {
      this.queryParams.factoryDeptCode = factoryDeptCode;
      this.queryParams.workshopId = workshopId;
      this.queryParams.workshopCode = workshopCode;
      this.queryParams.lineId = lineId;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        factoryDeptCode: null,
        workshopId: null,
        workshopCode: null,
        lineId: null,
        searchValue: null,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.detailDialog.row = row || null;
      this.detailDialog.disabled = false;
      this.detailDialog.visible = true;
    },
    /** 禁用/启用按钮操作 */
    handleDisabled(row) {
      if (!row) {
        return;
      }
      let msg = "";
      let isDisabled = row.disableFlag == "Y";
      if (isDisabled) {
        // 禁用
        msg = `确定启用${row.indexConfigName}吗？`;
      } else {
        // 启用
        msg = `确定禁用${row.indexConfigName}吗？`;
      }
      this.$confirm(msg, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          const param = {
            id: row.id,
            disableFlag: isDisabled ? "N" : "Y",
          };
          return disabledIndexConfig(param);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        });
    },
    /** 查看详情按钮操作 */
    handleShowDetail(row) {
      this.detailDialog.row = row;
      this.detailDialog.disabled = true;
      this.detailDialog.visible = true;
    },
  },
};
</script>
<style lang="scss" scoped>
.top {
  height: 44px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.pagination-container {
  margin: 0;
}
.status-Y {
  color: #d9001b;
  font-weight: 600;
}
.status-N {
  color: #88c446;
  font-weight: 600;
}
</style>
